<?php
use Illuminate\Support\Facades\DB;

$deviceCounts = DB::table('device')
    ->join('category', 'device.Category_id', '=', 'category.id')
    ->select('category.category', DB::raw('COUNT(device.id) as total'))
    ->groupBy('category.category')
    ->get();

    $category = [];
    $count = [];
    foreach ($deviceCounts as $deviceCount) {
        $category[] = $deviceCount->category;
        $count[] = $deviceCount->total;
    }

    
    $category = json_encode($category, JSON_UNESCAPED_UNICODE);
    $count = json_encode($count, JSON_UNESCAPED_UNICODE);
    // echo $category.$count;

?>


 <script src="/vendor/js/echarts.min.js"></script>
 <div id="devicemain" style="height:200px;box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);margin-bottom:30px;"></div>
 <script type="text/javascript">
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById('devicemain'));

      
      // 指定图表的配置项和数据
      var option = {
        title: {
          text: '设备分类数量',
          //left: 'center'
        },
        tooltip: {},
        legend: {
          data: ['分类']
        },
        xAxis: {
          data:   <?php echo $category; ?>
        },
        yAxis: {},
        series: [
          {
            name: '分类',
            type: 'bar',
            data:   <?php echo $count; ?>,
            label: {
                 show: true
            }
          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    </script>
